package com.feicheng.common.config;

import java.nio.charset.StandardCharsets;

import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.ResourceUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.feicheng.common.advice.TokenAspect;

import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
public class SystemConfig implements WebMvcConfigurer {


	@Bean(name="myaspect")
	public TokenAspect initMyaspect() {
		return new TokenAspect();
	}

	@Bean
	CORSFilter getCORSFilter(){
		return new CORSFilter();
	}

	@Bean("corsFilter")
	public FilterRegistrationBean<CORSFilter> registerTokenFilter(CORSFilter corsFilter) {
		FilterRegistrationBean<CORSFilter> registrationBean = new FilterRegistrationBean();
		registrationBean.setFilter(corsFilter);
		registrationBean.addUrlPatterns("/*"); //url拦截
		registrationBean.setOrder(1);
		registrationBean.setAsyncSupported(true);
		return registrationBean;
	}

	@Bean
	TokenFilter getTokenFilter(){
		return new TokenFilter();
	}

	@Bean("tokenFilter")
	public FilterRegistrationBean<TokenFilter> registerTokenFilter(TokenFilter tokenFilter) {
		FilterRegistrationBean<TokenFilter> registrationBean = new FilterRegistrationBean();
		registrationBean.setFilter(tokenFilter);
		registrationBean.addUrlPatterns("/*"); //url拦截
		registrationBean.setOrder(2);
		registrationBean.setAsyncSupported(true);
		return registrationBean;
	}
	
	@Bean
    public ServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory fa = new TomcatServletWebServerFactory();
        fa.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> connector.setProperty("relaxedQueryChars", "[]{}"));
        fa.addAdditionalTomcatConnectors(createStandardConnector());
        return fa;
    }
	
	@Value("${server.custom.httpPort}")
    private Integer httpPort;


    /**
     * 配置http
     * @return
     */
    private Connector createStandardConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setPort(httpPort);
        return connector;
    }
	
	@Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory){
		RestTemplate restTemplate = new RestTemplate(factory);
		restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;
    }
 
    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        //客户端与服务端建立连接超时时间
        factory.setConnectTimeout(100000);
        //客户端从服务端读取数据的超时时间
        factory.setReadTimeout(200000);
        return factory;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/");
    }
}
